<?php

/**
 * Description of IndexController
 *
 * @author Admin
 */
class Dataform_LiabilityController extends Zend_Controller_Action
{
    protected $_model;
    protected $_authService;

    public function init()
    {
        $layout = $this->_helper->layout();
        $layout->setLayout('main');

        $this->_model = new Dataform_Model_Liability();     // for Liability data

        $this->_authService = new Main_Service_Authentication();

        // add forms
        $this->view->dataform = $this->getDataForm();
    }

    /**
     * shows the list of all form fields for a  Liability
     */
    public function indexAction()
    {
        // checking if user is logged in
        if(!$this->_authService->getAuth()->hasIdentity())
            $this->_redirect('/');

        $this->view->liability_data = $this->_model->getAllLiabilityData();

    }

    public function getDataForm()
    {
        return $this->_forms['dataform'] = $this->_model->getForm('LiabilityDataform');
    }

    public function newAction()
    {
        if(!$this->_authService->getAuth()->hasIdentity())
            $this->_redirect('/');

        $this->_helper->FlashMessenger('')->clearMessages();
        $this->_helper->FlashMessenger('')->setNamespace('header')->addMessage('New Form Field');
        $this->_forward('edit');
    }

    /**
     * Use to show the edit/create form
     */
    public function editAction()
    {
        if(!$this->_authService->getAuth()->hasIdentity())
            $this->_redirect('/');

        $requst_params = $this->getRequest()->getParams();

        if( isset($requst_params['id']) && $requst_params['id'] !='' )
        {
            $this->_helper->FlashMessenger('')->clearMessages();
            $this->_helper->FlashMessenger('')->setNamespace('header')->addMessage('Edit Form Field');
            $id  =  $requst_params['id'];
            $this->view->form_field = $this->_model->getLiabilityDataById($id);

            $this->view->dataform = $this->getDataForm()->populate( $this->view->form_field->toArray() );
        }
    }

    public function saveAction()
    {
        $request = $this->getRequest();
        if (!$request->isPost()) {
            return $this->_helper->redirector('index');
        }
        $logger = Zend_Registry::get('log');
        $logger->log(print_r($request->getPost() ), Zend_Log::INFO );
        
        if (false === $this->_model->saveLiabilityField($request->getPost())  )
        {
            $this->_helper->FlashMessenger('')->setNamespace('error')->addMessage('An error Occured');
            return $this->render('edit');
        }

        $this->_helper->FlashMessenger('')->setNamespace('success')->addMessage('Form field saved successfuly !');
        return $this->_redirect('/dataform/liability/index');
    }

    public function deleteAction()
    {
        if(!$this->_authService->getAuth()->hasIdentity())
            $this->_redirect('/');

         if (false === ($id = $this->_getParam('id',false))) {
            throw new WOW_Exception('Unknown form field');
        }

        if($this->_model->deleteLiabilityData($id))
            $this->_helper->FlashMessenger('')->setNamespace('success')->addMessage('form field Deleted');
        else
            $this->_helper->FlashMessenger('')->setNamespace('Error')->addMessage('An error occured');

         return $this->_redirect('/dataform/liability/index');

    }

}